<!-- 报名表单页 -->
<template>
  <zh-app style="font-family: '阿里巴巴普惠体'">
    <!-- 导航栏 -->
    <zh-navbar title="义务段报名" color="#85a5ff" linear="right" />
    <zh-spin :load="data.loading" :tip="data.loadType === '1' ? '学生信息加载中，请耐心等候' : '报名信息提交中，请耐心等候'">
      <zh-sheet v-if="!form.canEnrollFlag" :round="5" :margin="[30, 30, 30, 0]" :padding="[20, 20]" text color="primary">
        <zh-text class="text-weight-b" label="温馨提示:"></zh-text>
        <zh-text style="text-indent: 2em; padding-top: 10rpx"
          >您目前没有取得<span style="color: red">东阳市公安局核发的处于有效期内的《浙江省居住证》</span>，不符合东阳义务段学校新生报名最低条件</zh-text
        >
        <zh-text style="text-indent: 2em; padding-top: 10rpx" color="red" label="若信息有误，请致电相关学校。"></zh-text>
      </zh-sheet>
      <zh-result v-if="form.canEnrollFlag && form.bmFlag" btnText="前往" title="已报名" subTitle="可前往我的报名查看结果" @click="onOk"></zh-result>
      <zh-form
        v-if="form.canEnrollFlag && !form.bmFlag"
        ref="formRef"
        :transprent="true"
        @submit="submit"
        v-model="form"
        inputAlign="right"
        :label-width="180"
        :margin="[0, 24]"
        :padding="[10, 0]"
      >
        <studentBase v-model="form"></studentBase>
        <enrollInfo v-model="form" v-if="!form.zsbEnrollFlag" :schoolId="data.schoolId" :beforeId="data.enrollBeforeId"></enrollInfo>
        <followInfo v-if="form.schoolNature==='0'&&!form.zsbEnrollFlag" v-model="form"></followInfo>
        <zsbEnrollInfo v-if="form.privateAppointFlag === '1' && form.zsbVO && form.enrollFlag && form.configTypeName" v-model="form"></zsbEnrollInfo>
        <mzgEnrollInfo v-if="form.enrollFlag && form.configTypeName&&form.showMzgFlag&&!form.zsbEnrollFlag" v-model="form"></mzgEnrollInfo>
        <mbEnrollInfo v-if="form.enrollFlag && form.configTypeName&&form.showMbFlag&&!form.zsbEnrollFlag" v-model="form"></mbEnrollInfo>
        <zh-sheet
          v-if="form.enrollFlag && form.privateAppointFlag === '1' && form.zsbFlag !== '1' && form.configTypeName&&!form.zsbEnrollFlag"
          :margin="[32, 0, 32, 24]"
          :padding="[24, 20]"
          :shadow="2"
          :round="6"
          color="red"
          text
        >
          <zh-checkbox :round="25" v-model="data.abandonZsbFlag">
            <template v-slot:default>
              <view class="flex flex-row">
                <zh-text
                  color="black"
                  label="我承诺放弃“九年一贯制“学校直升机会，改为参与其他学校的电脑派位录取或公办学校的按学区录取。我已阅读并理解本条内容，同时承诺放弃直升的一切后果自负。"
                ></zh-text>
              </view>
            </template>
          </zh-checkbox>
        </zh-sheet>
        <zh-form-item :border="false">
          <view class="flex flex-row">
            <view class="flex-1 mr-32" style="margin: 0 auto">
              <zh-button
                v-if="form.configTypeName"
                form-type="submit"
                :disabled="data.btnDisabled"
                :loading="data.btnDisabled"
                label="提交信息"
                block
                color="#85a5ff"
                linear="right"
              ></zh-button>
              <zh-button v-else label="返回" block color="grey"  :followTheme="false" @click="onCancel"></zh-button>
            </view>
          </view>
        </zh-form-item>
      </zh-form>
    </zh-spin>
    <zh-message ref="messageRef" :duration="3000" :width="400" :height="400" :lines="5"></zh-message>
    <zh-modal
      title="报名成功"
      :content="data.successMsg"
      cancelText="关闭页面"
      okText="前往我的报名"
      :overlayClick="false"
      v-model:show="data.showSuccess"
      @ok="onOk"
      @cancel="onCancel"
    ></zh-modal>
    <zh-modal
      title="是否确认提交?"
      content="提交后将不能修改报名信息!"
      cancelText="取消"
      okText="确认提交"
      :overlayClick="false"
      v-model:show="data.showSave"
      @ok="confirmBm"
    ></zh-modal>
  </zh-app>
</template>
<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue';
import studentBase from './components/studentBase.vue';
import enrollInfo from './components/enrollInfo.vue';
import zsbEnrollInfo from './components/zsbEnrollInfo.vue';
import mzgEnrollInfo from './components/mzgEnrollInfo.vue';
import mbEnrollInfo from './components/mbEnrollInfo.vue';
import followInfo from './components/followInfo.vue';
import * as util from '@/tool/function/util';
import { onLoad } from '@dcloudio/uni-app';
// ------------------接收传入参数(组件)-------------------

// ------------------钩子函数----------------------------
// 初始化
onLoad((options: any) => {
  if (options.schoolId) {
    data.schoolId = options.schoolId;
  }
  if (options.enrollBeforeId) {
    data.enrollBeforeId = options.enrollBeforeId;
  }
  if (options.configType) {
    data.configType = options.configType;
  }
  if (options.configTypeName) {
    data.configTypeName = options.configTypeName;
  }
  if (options.studentEnrollPreId) {
    data.studentEnrollPreId = options.studentEnrollPreId;
    getStudentInfo();
  }
});

// ------------------变量区------------------------------
let formRef = ref();
let messageRef = ref();
// 主数据
const data = reactive({
  schoolId: '',
  enrollBeforeId: '',
  studentEnrollPreId: '',
  btnDisabled: false,
  loading: false,
  loadType: '1', //1查询 2 保存
  showSuccess: false,
  successMsg: '', //报名成功提示
  configType: '',
  configTypeName: '',
  abandonZsbFlag: false, //放弃直升
  showSave: false, //提交前确认
  saveForm: {}, //保存数据
});
let form = ref<any>({});
// ------------------方法区------------------------------
const getStudentInfo = () => {
  //获取学生信息详情
  data.loading = true;
  data.loadType = '1';
  const params = { studentEnrollPreId: data.studentEnrollPreId };
  $api.common.send
    .post('eduStudentEnroll0028', params)
    .then((res: any) => {
      let student = res.result || {};
      //处理网签合同信息
      if (student.wqList && student.wqList.length > 0) {
        student.wqList = student.wqList.filter((item: any) => !item.schoolId || item.schoolId === data.schoolId);
      }
      if(student.enrollFlag&&student.privateAppointFlag==='1'){
        student.zsbFlag='1'
        console.log(student.zsbEnrollFlag)
        if(student.zsbEnrollFlag){
          student.configTypeName='直升'
        }
      }
      form.value = Object.assign({ mzgFlag: '0', zsbFlag: '0', mbFlag: '0', followFlag: '0' }, student);
      
      data.loading = false;
    })
    .catch((err: any) => {
      data.loading = false;
    });
};
// 提交
const submit = (e: any) => {
  if (e.validate) {
    let flag = false;
    let messageData: any = {};
    messageData.model = 'warn';
    messageData.text = '请输入必填信息';
    if (form.value.cardType === '01' && !util.isIdCard(form.value.idcard)) {
      messageData.text = '请输入正确的学生身份证！';
      messageRef.value.show(messageData);
      return;
    }
    let fimalyFlag = false;
    if (form.value.fatherIdcard) {
      if (form.value.fatherCardType === '01' && !util.isIdCard(form.value.fatherIdcard)) {
        messageData.text = '请输入正确的父亲（监护人）身份证！';
        messageRef.value.show(messageData);
        return;
      }
      fimalyFlag = true;
    }
    if (form.value.fatherCellphone) {
      if (!util.isPhone(form.value.fatherCellphone)) {
        messageData.text = '请输入正确的父亲（监护人）手机号码！';
        messageRef.value.show(messageData);
        return;
      }
    }

    if (form.value.motherIdcard) {
      if (form.value.motherCardType === '01' && !util.isIdCard(form.value.motherIdcard)) {
        messageData.text = '请输入正确的母亲身份证！';
        messageRef.value.show(messageData);
        return;
      }
      fimalyFlag = true;
    }
    if (form.value.motherCellphone) {
      if (!util.isPhone(form.value.motherCellphone)) {
        messageData.text = '请输入正确的母亲手机号码！';
        messageRef.value.show(messageData);
        return;
      }
    }
    if (!fimalyFlag) {
      messageData.text = '父亲（监护人）或母亲信息至少输入一方！';
      messageRef.value.show(messageData);
      return;
    }
    let saveForm = Object.assign({}, form.value) as any;
    if (form.value.fieldList) {
      for (var i = 0; i < form.value.fieldList.length; i++) {
        let fieldParam: any = form.value.fieldList[i];
        if (fieldParam.propertyType === '1' || fieldParam.propertyType === '2' || fieldParam.propertyType === '3' || fieldParam.propertyType === '11') {
          if (fieldParam.value) {
            saveForm[toHump(fieldParam.param)] = fieldParam.value;
            if (fieldParam.propertyType === '3') {
              let files = fieldParam.value.split(',');
              if (fieldParam.min && fieldParam.min > 0) {
                if (files.length < fieldParam.min) {
                  messageData.text = '请上传至少' + fieldParam.min + '张' + fieldParam.property + '相关材料！';
                  flag = true;
                  break;
                }
              }
            }
          } else {
            if (fieldParam.requiredFlag === '1') {
              messageData.text = fieldParam.property + '信息不能为空';
              flag = true;
              break;
            }
          }
        } else if (fieldParam.propertyType === '4' && fieldParam.requiredFlag === '1') {
          if (!form.value.studentStatusVO || !form.value.studentStatusVO.studentStatusId) {
            messageData.text = '该类别报名条件需要有东阳学籍，未获取到东阳学籍信息，如有疑问，请电话联系毕业学校';
            flag = true;
            break;
          }
        } else if (fieldParam.propertyType === '5' && fieldParam.requiredFlag === '1') {
          if (!form.value.houseHoldVO || !form.value.houseHoldVO.householdId) {
            messageData.text = '该类别报名条件需要有东阳户籍，未获户籍信息，如有疑问，请电话联系报名学校';
            flag = true;
            break;
          }
        } else if (fieldParam.propertyType === '6' && fieldParam.requiredFlag === '1') {
          if (!form.value.houseDsjId) {
            messageData.text = '该类别报名条件需要在东阳有房产，未获东阳房产信息，如有疑问，请电话联系报名学校';
            flag = true;
            break;
          }
        } else if (fieldParam.propertyType === '7' && fieldParam.requiredFlag === '1') {
          if (!form.value.netsignIdDsj) {
            messageData.text = '该类别报名条件需要有购房合同信息，未获取到购房合同信息，如有疑问，请电话联系报名学校';
            flag = true;
            break;
          }
        } else if (fieldParam.propertyType === '8' && fieldParam.requiredFlag === '1') {
          if (!form.value.socialSecurityIdDsj) {
            messageData.text = '该类别报名条件需要在东阳连续参保缴费6个月职工养老信息，未获东阳连续6个月职工养老参保信息，不能报名';
            flag = true;
            break;
          }
        } else if (fieldParam.propertyType === '9' && fieldParam.requiredFlag === '1') {
          if (!form.value.companyIdDsj) {
            messageData.text = '该类别报名条件需要有营业执照信息，未获开办企业信息，不能报名';
            flag = true;
            break;
          }
        } else if (fieldParam.propertyType === '10' && fieldParam.requiredFlag === '1') {
          if (!form.value.livePlaceIdDsj) {
            messageData.text = '该类别报名条件需要东阳居住证信息，未获东阳居住证信息，不能报名';
            flag = true;
            break;
          }
        }
        if(fieldParam.propertyType === '6'){
          saveForm.houseId=form.value.houseDsjId
        }else if(fieldParam.propertyType === '7'){
          saveForm.netsignId=form.value.netsignIdDsj
        }else if(fieldParam.propertyType === '8'){
          saveForm.socialSecurityId=form.value.socialSecurityIdDsj
        }else if(fieldParam.propertyType === '9'){
          saveForm.companyId=form.value.companyIdDsj
        }else if(fieldParam.propertyType === '10'){
          saveForm.livePlaceId=form.value.livePlaceIdDsj
        }
      }
    }

    if (flag) {
      messageRef.value.show(messageData);
      return;
    }
    if (form.value.schoolNature === '0') {
      //公办学校需判断报名类别不能为空
      if (!form.value.formId&&!form.value.qbFlag) {
        messageData.text = '请选择报名类别';
        messageRef.value.show(messageData);
        return;
      }
    }
    if (form.value.followFlag === '1') {
      //长幼随学
      if(!form.value.followIdcard){
        messageData.text = '请输入兄/姐身份证号码';
        messageRef.value.show(messageData);
        return;
      }
      if(!form.value.followSchoolId){
        messageData.text = '请选择长幼随学的学校';
        messageRef.value.show(messageData);
        return;
      }
    }
    if (form.value.enrollFlag&&form.value.privateAppointFlag === '1' && form.value.zsbFlag !== '1') {
      if(form.value.zsbEnrollFlag){
        messageData.text = '您选择放弃直升，不能提交';
        messageRef.value.show(messageData);
        return;
      }
      if (!data.abandonZsbFlag) {
        messageData.text = '您选择放弃直升，请先勾选同意放弃';
        messageRef.value.show(messageData);
        return;
      }
      if (data.abandonZsbFlag) {
        saveForm.abandonZsbFlag = '1';
      } else {
        saveForm.abandonZsbFlag = '0';
      }
    }
    let otherSchoolList = [] as any;
    if (form.value.zsbFlag === '1' && form.value.privateAppointFlag === '1') {
      if(!form.value.zsbEnrollFlag){
        let zsbVO = form.value.zsbVO;
        let zsbForm = {} as any;
        zsbForm.schoolId = zsbVO.schoolId;
        zsbForm.configTypeName = zsbVO.configTypeName;
        if (!zsbForm.configTypeName) {
          messageData.text = '直升班报名类别不能为空';
          messageRef.value.show(messageData);
          return;
        }
        otherSchoolList.push(zsbForm);
      }else{
        let zsbVO = form.value.zsbVO;
        saveForm.schoolId=zsbVO.schoolId
        saveForm.configTypeName = zsbVO.configTypeName;
        if (!saveForm.configTypeName) {
          messageData.text = '直升班报名类别不能为空';
          messageRef.value.show(messageData);
          return;
        }
      }
    } else {
      if (form.value.mzgFlag === '1' ) {
        if(!form.value.mzgInfo.schoolId){
          messageData.text = '请选择“公参民转公”学校';
          messageRef.value.show(messageData);
          return;
        }
        if (!form.value.mzgInfo.configTypeName) {
          messageData.text = '请选择“公参民转公”学校报名类别';
          messageRef.value.show(messageData);
          return;
        }
        if (form.value.mzgInfo.bindFlag==='1'&&(!form.value.mzgInfo.bindIdcardList||form.value.mzgInfo.bindIdcardList.length<=0)) {
          messageData.text = '请勾选绑定的学生';
          messageRef.value.show(messageData);
          return;
        }
        otherSchoolList.push(form.value.mzgInfo);
      }
      if (form.value.mbFlag === '1') {
        if (!form.value.mb1Info.schoolId) {
          messageData.text = '请选择民办第一志愿学校';
          messageRef.value.show(messageData);
          return;
        }
        if (form.value.mb3Info.schoolId&&!form.value.mb2Info.schoolId) {
          messageData.text = '请选择调剂志愿1学校';
          messageRef.value.show(messageData);
          return;
        }
        if (form.value.mb1Info.schoolId === form.value.mb2Info.schoolId) {
          messageData.text = '民办第一志愿和调剂志愿1学校不能选一样';
          messageRef.value.show(messageData);
          return;
        }
        if (form.value.mb1Info.schoolId === form.value.mb3Info.schoolId) {
          messageData.text = '民办第一志愿和调剂志愿2学校不能选一样';
          messageRef.value.show(messageData);
          return;
        }
        if(form.value.mb2Info.schoolId||form.value.mb3Info.schoolId){
          if(!form.value.mb1Info.schoolId){
            messageData.text = '选择报民办学校第一志愿必填';
            messageRef.value.show(messageData);
            return;
          }
          if (form.value.mb2Info.schoolId === form.value.mb3Info.schoolId) {
            messageData.text = '调剂志愿1和调剂志愿2学校不能选一样';
            messageRef.value.show(messageData);
            return;
          }
        }
        
        if (form.value.mb1Info.schoolId) {
          if (!form.value.mb1Info.configTypeName) {
            messageData.text = '请选择民办第一志愿报名类别';
            messageRef.value.show(messageData);
            return;
          }
          otherSchoolList.push(form.value.mb1Info);
        }
        if (form.value.mb2Info.schoolId) {
          if (!form.value.mb2Info.configTypeName) {
            messageData.text = '请选择民办第二志愿报名类别';
            messageRef.value.show(messageData);
            return;
          }
          otherSchoolList.push(form.value.mb2Info);
        }
        if (form.value.mb3Info.schoolId) {
          if (!form.value.mb3Info.configTypeName) {
            messageData.text = '请选择民办第三志愿报名类别';
            messageRef.value.show(messageData);
            return;
          }
          otherSchoolList.push(form.value.mb3Info);
        }
      }
    }
    saveForm.otherSchoolList = otherSchoolList;
    data.saveForm = saveForm;
    data.showSave = true;
  } else {
    let messageData: any = {};
    messageData.model = 'warn';
    messageData.text = '请核对页面表单信息，带红色星号的内容必填！';
    messageRef.value.show(messageData);
    return;
  }
};
const confirmBm = () => {
  data.showSave = false;
  //报名提交
  data.btnDisabled = true;
  data.loadType = '2';
  data.loading = true;
  $api.common.send
    .post('eduStudentEnroll0019', data.saveForm)
    .then((res: any) => {
      //保存成功后提示
      data.btnDisabled = false;
      data.loading = false;
      $router.replaceAll('/pages/edu/enroll/successEnroll', {applyId:res.result.applyId});
      //data.successMsg = '您的报名编号为：' + res.result.applyId;
      //data.showSuccess = true;
    })
    .catch(() => {
      data.btnDisabled = false;
      data.loading = false;
    });
};
const onOk = () => {
  //前往我的报名
  $router.replaceAll('/pages/edu/enroll/myEnroll', {});
};
const onCancel = () => {
  //关闭页面
  $router.replaceAll('/pages/edu/theRightChild/theRightChild', {});
};
const toHump = (name: String) => {
  return name.replace(/\_(\w)/g, function (all, letter) {
    return letter.toUpperCase();
  });
};
const getUploadCount = (item: any) => {
  if (item && item.value) {
    return item.value.split(',').length;
  } else {
    return 0;
  }
};
</script>
<style lang="scss" scoped>
.card {
  width: 630rpx;
  margin: 24rpx 32rpx;
  padding: 34rpx 26rpx 26rpx;
  border-radius: 8rpx;
  background-image: linear-gradient(to right, #f8faff, #ebf1ff);
  box-shadow: rgb(0 85 255 / 20%) 0px 2px 6px;

  .title {
    color: #6e6e6e;
    font-size: 34rpx;
    font-weight: 600;
    padding-left: 20rpx;
    border-left: 10rpx solid #67b6ff;
  }

  .desc {
    color: #9e9e9e;
    font-size: 26rpx;
    font-weight: 600;
    margin-top: 20rpx;
  }

  span {
    color: #5db1ff;
    font-weight: 600;
  }

  .form {
  }
}
</style>
